import java.awt.Color;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.function.Function2D;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.RefineryUtilities;

public class FunctionDemo1 extends ApplicationFrame {

	public FunctionDemo1(String title) {
		super(title);
		JPanel chartPanel = createDemoPanel();
		chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
		setContentPane(chartPanel);
	}

	private static XYDataset createDataset1() {
		Function2D f1 = new Function2D() {
			public double getValue(double x) {
//				return 3 * x * x * x + x * x + 4.0;
				return 100 + 3 * x;
			}
		};
		return DatasetUtilities.sampleFunction2D(f1, -4.0, 2.5, 100, "f1");
	}

	private static XYDataset createDataset2() {
		Function2D f2 = new Function2D() {
			public double getValue(double x) {
				return 50 - 50 * x * x;
			}
		};
		return DatasetUtilities.sampleFunction2D(f2, -2.0, 2.0, 300, "f2");
	}

	private static JFreeChart createChart(XYDataset dataset1/*, XYDataset dataset2*/) {
		// create the chart...
		JFreeChart chart = ChartFactory.createXYLineChart(
				"Function Chart Demo", // chart title
				"X", // x axis label
				"Y", // y axis label
				dataset1, // data
				PlotOrientation.VERTICAL, true, // include legend
				true, // tooltips
				false // urls
				);

		// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
		chart.setBackgroundPaint(Color.white);

		// get a reference to the plot for further customisation...
		XYPlot plot = (XYPlot) chart.getPlot();
//		plot.setDataset(1, dataset2);
		plot.setBackgroundPaint(Color.lightGray);
		plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
		plot.setDomainGridlinePaint(Color.white);
		plot.setRangeGridlinePaint(Color.white);

		XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot
				.getRenderer();
		renderer.setShapesVisible(false);

		XYLineAndShapeRenderer renderer2 = new XYLineAndShapeRenderer(true,
				false);
		plot.setRenderer(1, renderer2);

		// change the auto tick unit selection to integer units only...
		NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		// OPTIONAL CUSTOMISATION COMPLETED.

		return chart;
	}

	public static JPanel createDemoPanel() {
		JFreeChart chart = createChart(createDataset1()/*, createDataset2()*/);
		return new ChartPanel(chart);
	}

	public static void main(String[] args) {
		FunctionDemo1 demo = new FunctionDemo1("Function Demo 1");
		demo.pack();
		RefineryUtilities.centerFrameOnScreen(demo);
		demo.setVisible(true);
	}

}